using DAO;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Easislides
{
	public class FrmLookupTitles : Form
	{
		private const string Reg_LookupIncludeBookRef = "LookupIncludeBookRef";
		private const string Reg_LookupIncludeUserRef = "LookupIncludeUserRef";
		private IContainer components = null;
		private ListView SongsList;
		private ColumnHeader columnHeader1;
		private ColumnHeader columnHeader2;
		private ColumnHeader columnHeader3;
		private ColumnHeader columnHeader4;
		private Label label1;
		private CheckBox cbBookRef;
		private CheckBox cbUserRef;
		private Button BtnCancel;
		private Button BtnOK;
		private ColumnHeader columnHeader5;
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
            this.SongsList = new System.Windows.Forms.ListView();
            this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader4 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader5 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.label1 = new System.Windows.Forms.Label();
            this.cbBookRef = new System.Windows.Forms.CheckBox();
            this.cbUserRef = new System.Windows.Forms.CheckBox();
            this.BtnCancel = new System.Windows.Forms.Button();
            this.BtnOK = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // SongsList
            // 
            this.SongsList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1,
            this.columnHeader2,
            this.columnHeader3,
            this.columnHeader4,
            this.columnHeader5});
            this.SongsList.FullRowSelect = true;
            this.SongsList.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
            this.SongsList.Location = new System.Drawing.Point(12, 41);
            this.SongsList.MultiSelect = false;
            this.SongsList.Name = "SongsList";
            this.SongsList.ShowItemToolTips = true;
            this.SongsList.Size = new System.Drawing.Size(451, 183);
            this.SongsList.TabIndex = 1;
            this.SongsList.UseCompatibleStateImageBehavior = false;
            this.SongsList.View = System.Windows.Forms.View.Details;
            this.SongsList.DoubleClick += new System.EventHandler(this.SongsList_DoubleClick);
            // 
            // columnHeader1
            // 
            this.columnHeader1.Text = "Song Title";
            this.columnHeader1.Width = 214;
            // 
            // columnHeader2
            // 
            this.columnHeader2.Text = "Song ID";
            this.columnHeader2.Width = 0;
            // 
            // columnHeader3
            // 
            this.columnHeader3.Text = "Folder";
            this.columnHeader3.Width = 92;
            // 
            // columnHeader4
            // 
            this.columnHeader4.Text = "Book Ref";
            this.columnHeader4.Width = 74;
            // 
            // columnHeader5
            // 
            this.columnHeader5.Text = "User Ref";
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(12, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(301, 27);
            this.label1.TabIndex = 0;
            this.label1.Text = "Song titles listing based on what you have typed under Title2.  Select ONE title " +
                "from the list and click OK to copy it to Title2.";
            // 
            // cbBookRef
            // 
            this.cbBookRef.AutoSize = true;
            this.cbBookRef.Location = new System.Drawing.Point(13, 230);
            this.cbBookRef.Name = "cbBookRef";
            this.cbBookRef.Size = new System.Drawing.Size(154, 17);
            this.cbBookRef.TabIndex = 2;
            this.cbBookRef.Text = "Also Copy Book Reference";
            // 
            // cbUserRef
            // 
            this.cbUserRef.AutoSize = true;
            this.cbUserRef.Location = new System.Drawing.Point(13, 245);
            this.cbUserRef.Name = "cbUserRef";
            this.cbUserRef.Size = new System.Drawing.Size(151, 17);
            this.cbUserRef.TabIndex = 3;
            this.cbUserRef.Text = "Also Copy User Reference";
            // 
            // BtnCancel
            // 
            this.BtnCancel.Location = new System.Drawing.Point(382, 238);
            this.BtnCancel.Name = "BtnCancel";
            this.BtnCancel.Size = new System.Drawing.Size(80, 24);
            this.BtnCancel.TabIndex = 5;
            this.BtnCancel.Text = "Cancel";
            this.BtnCancel.Click += new System.EventHandler(this.BtnCancel_Click);
            // 
            // BtnOK
            // 
            this.BtnOK.Location = new System.Drawing.Point(286, 238);
            this.BtnOK.Name = "BtnOK";
            this.BtnOK.Size = new System.Drawing.Size(80, 24);
            this.BtnOK.TabIndex = 4;
            this.BtnOK.Text = "OK";
            this.BtnOK.Click += new System.EventHandler(this.BtnOK_Click);
            // 
            // FrmLookupTitles
            // 
            this.ClientSize = new System.Drawing.Size(474, 274);
            this.Controls.Add(this.BtnCancel);
            this.Controls.Add(this.BtnOK);
            this.Controls.Add(this.cbUserRef);
            this.Controls.Add(this.cbBookRef);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.SongsList);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FrmLookupTitles";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Lookup Titles";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmLookupTitles_FormClosing);
            this.Load += new System.EventHandler(this.FrmLookupTitles_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		public FrmLookupTitles()
		{
			this.InitializeComponent();
		}
		private void FrmLookupTitles_Load(object sender, EventArgs e)
		{
			gf.Lookup_NameBookRef = "";
			gf.Lookup_NameUserRef = "";
			this.cbBookRef.Checked = (gf.GetRegValue("options", "LookupIncludeBookRef", 1) > 0);
			this.cbUserRef.Checked = (gf.GetRegValue("options", "LookupIncludeUserRef", 0) > 0);
			try
			{
				ListViewItem listViewItem = new ListViewItem();
				string fullSearchString = "select * from SONG where title_1 like \"" + gf.Lookup_NameSelected + "\" and folderno > 0 order by cjk_strokecount";
				Recordset recordSet = gf.GetRecordSet(gf.ConnectStringMainDB, fullSearchString);
				if (recordSet != null && !recordSet.EOF)
				{
					recordSet.MoveFirst();
					while (!recordSet.EOF)
					{
						listViewItem = this.SongsList.Items.Add(gf.ObjToString(recordSet.Fields["Title_1"].Value));
						listViewItem.SubItems.Add(gf.ObjToString(recordSet.Fields["SongID"].Value));
						listViewItem.SubItems.Add(gf.FolderName[gf.ObjToInt(recordSet.Fields["FolderNo"].Value)]);
						listViewItem.SubItems.Add(gf.ObjToString(recordSet.Fields["Book_Reference"].Value));
						listViewItem.SubItems.Add(gf.ObjToString(recordSet.Fields["User_Reference"].Value));
						recordSet.MoveNext();
					}
				}
			}
			catch
			{
			}
		}
		private void BtnOK_Click(object sender, EventArgs e)
		{
			this.SelectTitle_Click();
		}
		private void SongsList_DoubleClick(object sender, EventArgs e)
		{
			this.SelectTitle_Click();
		}
		private void SelectTitle_Click()
		{
			if (this.SongsList.SelectedItems.Count > 0)
			{
				int selectedIndex = gf.GetSelectedIndex(this.SongsList);
				if (selectedIndex >= 0)
				{
					gf.Lookup_NameSelected = this.SongsList.Items[selectedIndex].Text;
					if (this.cbBookRef.Checked)
					{
						gf.Lookup_NameBookRef = this.SongsList.Items[selectedIndex].SubItems[3].Text;
					}
					if (this.cbUserRef.Checked)
					{
						gf.Lookup_NameUserRef = this.SongsList.Items[selectedIndex].SubItems[4].Text;
					}
				}
				base.DialogResult = DialogResult.OK;
				base.Close();
			}
		}
		private void FrmLookupTitles_FormClosing(object sender, FormClosingEventArgs e)
		{
			gf.SaveRegValue("options", "LookupIncludeBookRef", this.cbBookRef.Checked ? 1 : 0);
			gf.SaveRegValue("options", "LookupIncludeUserRef", this.cbUserRef.Checked ? 1 : 0);
		}

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
	}
}
